package com.wash.shoes.controller.pc;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.R;
import com.wash.shoes.entity.ShoeRackGridPo;
import com.wash.shoes.entity.ShoeRackPo;
import com.wash.shoes.exception.WashException;
import com.wash.shoes.service.ShoeRackGridService;
import com.wash.shoes.service.ShoeRackService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ljg
 * @since 2024-08-27
 */
@Api(tags = {"PC-鞋架"})
@RestController
@RequestMapping({"/dev-api/shoeRack", "/shoeRack"})
public class ShoeRackController {

    @Autowired
    ShoeRackService shoeRackService;

    @Autowired
    ShoeRackGridService shoeRackGridService;

    @ApiOperation("获取鞋架列表")
    @PostMapping("/list")
    public R<Page<ShoeRackPo>> pageList(@RequestBody ShoeRackPo shoeRackPo) {
        Page<ShoeRackPo> page = shoeRackService.pageList(shoeRackPo);
        return R.ok(page);
    }

    @ApiOperation("获取鞋架列表每个用户可以看见的鞋架")
    @PostMapping("/userShoeRackList")
    public R<List<ShoeRackPo>> userShoeRackList() {
        List<ShoeRackPo> dataList = shoeRackService.userShoeRackList();
        return R.ok(dataList);
    }

    @ApiOperation("修改新增鞋架,有id为修改没有为新增")
    @PostMapping("/saveUpdate")
    @Transactional
    public R<ShoeRackPo> add(@RequestBody ShoeRackPo shoeRackPo) {
        if (ObjectUtil.isEmpty(shoeRackPo.getId())) {
            shoeRackPo.setStatus(1);

            List<ShoeRackPo> dataList = shoeRackService.getByCodeId(shoeRackPo.getRackCode(), null);
            if (!dataList.isEmpty()) {
                throw new WashException("鞋架编码存在");
            }
            shoeRackService.save(shoeRackPo);
            List<ShoeRackGridPo> shoeRackGridPoList = new ArrayList<>(shoeRackPo.getColNum() * shoeRackPo.getRowNum());
            // 添加货架，还需要添加对应的格子
            for (int i = 0; i < shoeRackPo.getRowNum(); i++) {
                for (int j = 0; j < shoeRackPo.getColNum(); j++) {
                    ShoeRackGridPo shoeRackGridPo = new ShoeRackGridPo();
                    shoeRackGridPo.setShopId(shoeRackPo.getShopId());
                    shoeRackGridPo.setShoeRackId(shoeRackPo.getId());
                    shoeRackGridPo.setRowNumberSort(i);
                    shoeRackGridPo.setColNumberSort(j);
                    shoeRackGridPo.setSortNum(i * shoeRackPo.getColNum() + j);
                    shoeRackGridPo.setShowCode(shoeRackPo.getRackCode() + "-" + (i + 1) + "-" + (j + 1));
                    shoeRackGridPoList.add(shoeRackGridPo);
                }
            }
            shoeRackGridService.saveOrUpdateBatch(shoeRackGridPoList);
        } else {
            // 更新不能更新其他的，只能更新名字
            List<ShoeRackPo> dataList = shoeRackService.getByCodeId(shoeRackPo.getRackCode(), shoeRackPo.getId());
            if (!dataList.isEmpty()) {
                throw new WashException("鞋架编码存在");
            }
            // 判断是否是下架
            List<ShoeRackGridPo> shoeRackGridPoList = shoeRackGridService.getRackGrid(shoeRackPo.getId());
            for (ShoeRackGridPo shoeRackGridPo : shoeRackGridPoList) {
                shoeRackGridPo.setStatus(shoeRackPo.getStatus());
            }
            // 更新格子的状态
            shoeRackGridService.updateBatchById(shoeRackGridPoList);
            shoeRackService.updateById(shoeRackPo);
        }
        return R.ok(shoeRackPo);
    }

    @ApiOperation("逻辑删除 修改 status为0")
    @PostMapping("/deleteByStatus")
    public R<ShoeRackPo> deleteByStatus(String theId) {
        ShoeRackPo shoeRackPo = shoeRackService.getById(theId);
        List<ShoeRackGridPo> shoeRackGridPoList = shoeRackGridService.getRackGrid(theId);
        for (ShoeRackGridPo shoeRackGridPo : shoeRackGridPoList) {
            if (StrUtil.isNotBlank(shoeRackGridPo.getShoeId())) {
                throw new RuntimeException("请出库所有的鞋子");
            }
            shoeRackGridPo.setStatus(0);
        }
        // 所有的格子都设置未删除
        shoeRackGridService.updateBatchById(shoeRackGridPoList);
        shoeRackPo.setStatus(0);
        shoeRackService.updateById(shoeRackPo);
        // 修改状态为0 ，如果需要判断是否能删除，后期处理
        return R.ok(shoeRackPo);
    }

    @ApiOperation("获取详情")
    @PostMapping("/getDetailById")
    public R<ShoeRackPo> getDetailById(String theId) {
        ShoeRackPo shoeRackPo = shoeRackService.getById(theId);
        return R.ok(shoeRackPo);
    }


}

